<template>
    <rx-layout>
        <div slot="center" style>
            <rx-fit>
                <div slot="toolheader" foldheader="true" foldbtn="false" border="false">
                    <div class="table-operator">
                        <rx-button alias="customPageTemplateAdd" :butn-icon="'plus'" :butn-type="'primary'"
                                   :show-no-right="true" @click="add">{{$ti18('rxList.newBtn',"新建")}}
                        </rx-button>
                        <a-dropdown
                            v-if="editEnable && selectedRowKeys.length > 0 || removeEnable && selectedRowKeys.length > 0">
                            <a-button style="margin-left: 8px"> {{$ti18('rxList.moreBtn',"更多")}}
                                <a-icon type="down"/>
                            </a-button>
                            <a-menu slot="overlay">
                                <a-menu-item key="1" v-if="editEnable && selectedRowKeys.length > 0"
                                             alias="customPageTemplateEdit" :butn-icon="'edit'"
                                             :butn-type="'primary'" :show-no-right="false" @click="editOne">
                                    <a-icon type="edit"/>
                                    {{$ti18('rxList.editBtn',"编辑")}}
                                </a-menu-item>
                                <a-menu-item key="2" v-if="removeEnable && selectedRowKeys.length > 0"
                                             alias="customPageTemplateDelete" :butn-icon="'delete'"
                                             :butn-type="'danger'"
                                             :show-no-right="false" @click="delByIds(selectedRowKeys)">
                                    <a-icon type="delete"/>
                                    {{$ti18('rxList.delBtn',"删除")}}
                                </a-menu-item>
                                <a-menu-item
                                    key="3"
                                    @click="changeExport(exportConf)"
                                    v-if="selectedRowKeys.length > 0"
                                    alias="formRuleDelete"
                                    :butn-type="'primary'"
                                    :show-no-right="false"
                                >
                                    <a-icon type="switcher"/>
                                    {{$ti18('rxList.changeExport','切换导出')}}
                                </a-menu-item>
                            </a-menu>
                        </a-dropdown>
                    </div>
                    <span class="search-btn-box">
                        <span class="search-btn" @click="searchshow"><i class="iconfont iconguolv"></i>
                        {{$ti18('rxList.leachBtn','过滤')}}
                        </span>
                    </span>
                </div>
                <div slot="searchbar" btnalign="right" v-if="fitSearch" @search="search" @cancel="cancel"
                     @close="close">
                    <a-form layout="vertical">
                        <a-form-item :label="$ti18('rxForm.name','名称')">
                            <a-input :placeholder="$ti18('rxForm.name','名称','','input')" v-model="queryParam.Q_NAME__S_LK"/>
                        </a-form-item>
                        <a-form-item :label="i18n('form.alias','别名')">
                            <a-input :placeholder="i18n('form.alias','别名','input')" v-model="queryParam.Q_ALIAS__S_LK"/>
                        </a-form-item>
                    </a-form>
                </div>
                <rx-grid
                    ref="table"
                    :bordered="isBorder"
                    :allowRowSelect="true"
                    :multiSelect="true"
                    :columns="columns"
                    :defaultPageSize="10"
                    url="/api-portal/portal/core/customPageTemplate/query"
                    :queryParam="queryParam"
                    data-field="result.data"
                    :pageSizeOptions="['10','30','40']"
                    id-field="id"
                    @selectChange="onSelectChange"
                >
                    <span slot="isDefault" slot-scope="{text, record, index}">
                      <a-tag :color="defaultMap[text].color">
                        <span>{{defaultMap[text].text}}</span>
                      </a-tag>
                    </span>
                    <span slot="exportApp">
                         {{ exportI18n('export.isExport','导出') }}
                    </span>
                    <span slot="isExport" slot-scope="{ text, record }">
                        <a-tag :color="text == 1 ? 'green' : 'red'">
                            <span>{{ text == 1 ? exportI18n('export.yes','是') : exportI18n('export.no','否') }}</span>
                        </a-tag>
                    </span>
                    <span slot="action" slot-scope="{ text, record }" class="actionIcons">
                        <span  @click.stop="edit(record)">
                            {{$ti18('rxList.editBtn','编辑')}}
                        </span>
                        <span  @click.stop="onPreview(record)">
                            {{$ti18('rxList.previews','预览')}}
                        </span>
                        <span v-if="record.isDefault!='0'"  @click.stop="delByIds([record.id])">
                            {{$ti18('rxList.delBtn','删除')}}
                        </span>
                        <span @click="setExport(record,exportConf)">
                            {{record.isExport == 1 ? exportI18n('export.noExport','不导出') : exportI18n('export.exported','导出')}}
                        </span>
                    </span>
                </rx-grid>
            </rx-fit>
        </div>
    </rx-layout>
</template>

<script>
import CustomPageTemplateApi from '@/api/portal/core/customPageTemplate'
import CustomPageTemplateEdit from './CustomPageTemplateEdit'
import {BaseList, RxFit, RxGrid, Util} from "@lowcode/jpaas-common-lib";
import CustomPageShow from "@/views/modules/portal/core/CustomPageShow";
import switchExport from "@/utils/switchExport";

export default {
    name: 'CustomPageTemplateList',
    mixins: [BaseList,switchExport],
    components: {
        RxFit
    },
    data() {
        return {
            columns: [
                {
                    title: this.$ti18('rxList.index','序号'),
                    type: 'indexColumn',
                    width: 40,
                    dataIndex: 'serial',
                    scopedSlots: {customRender: 'serial'}
                },
                {title: this.$ti18('rxForm.name','名称'), dataIndex: 'name', width: 160},
                {title: this.i18n('form.alias','别名') , dataIndex: 'alias', width: 100},
                {
                    dataIndex: 'isExport',
                    slots: { title: 'exportApp' },
                    width: 60,
                    scopedSlots: {customRender: 'isExport'}
                },
                {title: this.$ti18('rxForm.createBy','创建人'), dataIndex: 'createByName', width: 100},
                {title: this.i18n('form.default','默认'), dataIndex: 'isDefault', width: 60,scopedSlots: {customRender: 'isDefault'}},
                {title: this.$ti18('rxForm.createTime','创建时间'), dataIndex: 'createTime',  width: 120},
                {title: this.$ti18('rxList.action','操作'), width: 180, dataIndex: 'action', scopedSlots: {customRender: 'action'}}
            ],
            component: CustomPageTemplateEdit,
            comment: this.i18n('list.comment','自定义页面模板') ,
            max:true,
            fitSearch: false,
            defaultMap: {
                0: {color: 'green', text: this.i18n('isDefaultMap.yes','是')},
                1: {color: 'red', text: this.i18n('isDefaultMap.no','否')},
            },
            /**
             * 导出配置
             */
            exportConf:{
                Api:CustomPageTemplateApi,
                id:"id"
            }
        }
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "customPageTemplate", key);
        },
        exportI18n(name,text,key){
            return this.$ti18(name,text,"appExport",key);
        },
        delById(ids) {
            return CustomPageTemplateApi.del({ids: ids.join(',')});
        },
        getListByParams(parameter) {
            return CustomPageTemplateApi.query(parameter)
        },
        onPreview(record){
            Util.open({
                component:CustomPageShow,
                curVm:this,
                title:this.i18n('form.customPagePreview','自定义页面预览') ,
                max:true,
                data:{
                    config:{alias:record.alias,name:record.name,type:"template"},
                    type:record.type
                }
            },function (action){
            });
        },
        delByIds(ids) {
            let self_ = this;
            let ids_=[];
            for (let i = 0; i < self_.selectedRows.length; i++) {
                if(self_.selectedRows[i].isDefault=="1"){
                    ids_.push(self_.selectedRows[i].id);
                }
            }
            ids= [...new Set([...ids,...ids_])];
            this.$confirm({
                title: this.$ti18('rxMsa.actionTips','操作提示'),
                zIndex: 20000,
                content: this.i18n('form.deleteTheSelectedRecord','您确定需要删除选中的记录吗(默认不允许删除)？') ,
                okText: this.$ti18('rxForm.ackBtn','确认'),
                cancelText: this.$ti18('rxForm.cancelBtn','取消'),
                onOk() {
                    self_.delById(ids).then(res => {
                        if (res.success && res.code == 200) {
                            self_.refreshGrid()
                        }
                        self_.selectedRowKeys = [];
                        self_.selectedRows = [];
                    })
                },
                onCancel() {
                }
            })
        },
    }
}
</script>
<style scoped>

</style>

